﻿using Rainbow.core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Navigation;

namespace Rainbow.model
{
    public class LocalPathTreeNode : TreeNode
    {
        public static LocalPathTreeNode _temp = new LocalPathTreeNode();
        public LocalPathTreeNode()
        {
            this.IsExpanded = false;
            this.IsSelected = false;
            Children = new ObservableCollection<TreeNode> ();
            Children.Add(_temp);
        }

        public void Reset()
        {
            Children.Clear();
            Children.Add(_temp);
        }

        public bool Equals(LocalPathTreeNode obj, object key)
        {
            return (string)key == obj.path;
        }

        private LocalPathTreeNode m_ParentNode;
        public LocalPathTreeNode ParentNode
        {
            get { return m_ParentNode; }
            set
            {
                m_ParentNode = value;
                NotifyPropertyChanged();
            }
        }

        private string m_displayName;
        public string displayName
        {
            get { return m_displayName; }
            set
            {
                m_displayName = value;
                NotifyPropertyChanged();
            }
        }

        private string m_path;
        public string path
        {
            get { return m_path; }
            set
            {
                if (m_path != value)
                {
                    m_path = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private bool m_IsSelected;
        public bool IsSelected
        {
            get { return m_IsSelected; }
            set
            {
                m_IsSelected = value;
                NotifyPropertyChanged();
            }
        }

        private bool m_IsExpanded;
        public bool IsExpanded
        {
            get { return m_IsExpanded; }
            set
            {
                if (value != m_IsExpanded)
                {
                    if (!HasChildren())
                    {
                        Children.Remove(_temp);
                        LoadChildren();
                    }
                    m_IsExpanded = value;
                    NotifyPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 判断是否有子节点（逻辑是：如果只有一个临时子节点，说明没有真正的子节点）
        /// </summary>
        /// <returns></returns>
        private bool HasChildren()
        {
            return !(Children.Count == 1 && Children[0] == _temp);
        }

        public void LoadChildren()
        {
            try
            {
                // 创建 DirectoryInfo 对象
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                DirectoryInfo[] pathList= directoryInfo.GetDirectories(); ;
                foreach (DirectoryInfo path in pathList)
                {
                    LocalPathTreeNode node = new LocalPathTreeNode();
                    node.Name = path.Name;
                    node.displayName = path.Name;
                    node.path = path.FullName;
                    node.ParentNode = this;
                    Children.Add(node);
                }
            }
            catch (Exception)
            {
               
            }
        }

        public void ChildReset()
        {
            Children.Clear();
            Children.Add(_temp);
        }
    }
}
